var app=getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {    
    banrdObj:{},
    next: true,
    list: [],
    page: 0,
    size: 10,
    shareShow:false,
    recordUrl:"/api/memberCoupon/couponListslByBanrdId.do",
    shareImage:"/lib/dute.mp/res/fxbgs.png",
    yjyqUrl: "/api/memberCoupon/getYjyqCouponData.do",
    qrCodeUrl: "/api/memberCoupon/smallProgramCode.do",
	action:"",
	submit:"{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu();
    var self = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth;
        self.setData({
          winheight: clientHeight,
          winwidth: clientWidth
        });
      }
    });
    this.setData({
      userInfo: wx.getStorageSync("userInfo"),
      id: wx.getStorageSync("banrdId")
    })
    var bparmas = { banrdId: parseInt(self.data.id) }
    ux.request("/api/banrd/getBanrdInfos.do", bparmas, function (protocol1) {
      wx.setStorageSync("banrd", protocol1.data.merchant);
      wx.setStorageSync("banrdId", self.data.id);
      self.setData({
        banrdObj: protocol1.data.merchant,
      })
    }, true);
  
    var pamars = { banrdId: this.data.id };
    var ppaa = { banrdId: self.data.id }
    ux.request(self.data.qrCodeUrl, ppaa, function (protocol) {
      if (protocol.data.qrcode){
        self.setData({
          qrcode: protocol.data.qrcode
        })
        self.downLoadImg(protocol.data.qrcode, 'qrcode');
      }
    }, true);
    self.getShar();
    self.downLoadImg(wx.getStorageSync("userInfo").header, 'header');
    ux.request(this.data.yjyqUrl, pamars, function (protocol) {
      self.setData({
        obj: protocol.data.obj
      })
    }, true);
    this.getList();
  },
  getShar:function(){
    var self=this;
    var banrdid=wx.getStorageSync('banrdId');
    ux.request("/api/Merchant/getShars.do", {banrd:parseInt(banrdid),type:4},function(data){
      if(data.data.obj){
        self.setData({
          fenxiang:data.data.obj
        })
      }
    });
  },
  getList: function () {
    var self = this;
    if (self.data.next == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { banrdId: this.data.id, pageNo: parseInt(self.data.page), pageSize: self.data.size };
    ux.request(self.data.recordUrl, params, function (data) {
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        self.setData({
          next: true
        });
      } else {
        self.setData({
          next: false
        });
      }
      if (self.data.page == 0) {
        self.setData({
          list: datas
        });
      } else {
        self.setData({
          list: self.data.list.concat(datas)
        });
      }
      var pages = self.data.page + 1;
      self.setData({
        page: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
  },
  downLoadImg: function (netUrl, storageKeyUrl) {
    wx.getImageInfo({
      src: netUrl,    //请求的网络图片路径
      success: function (res) {
        //请求成功后将会生成一个本地路径即res.path,然后将该路径缓存到storageKeyUrl关键字中
        // wx.setStorageSync("token", data.data.token);
        wx.setStorage({
          key: storageKeyUrl,
          data: res.path,
        });

      }
    })
  },
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,event.detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo({url:dataset.url});
					}
				}
		  },true);
	 }
  },
  
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this;
    wx.showShareMenu({
      withShareTicket: true
    })
    var userInfo = this.data.userInfo;
    var obj=this.data.obj;
    var shareObj = {
      title: this.data.fenxiang.shareTitle,
      path: '/pages/member/tab3/coupon/couponGet?banrdName=' + this.data.banrdObj.brandName+'&userId=' + userInfo.uid+"&banrdId="+this.data.id,
      imageUrl: this.data.fenxiang.url,
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
        that.toast("转发成功");
        }
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          that.toast("取消转发");
        } else if (res.errMsg == 'shareAppMessage:fail') {
          that.toast("转发失败");
        }
      
      }
    }
    return shareObj;
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  toFenxImg:function(){
    //   this.toast("该功能正在维护")
    //  return false;
      wx.navigateTo({
        url: '/pages/member/tab3/distribution/posterImgs',
      })
   
    
  }
})
